package com.qqtech.qquan.activity.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.agent.context.AdminPage;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.activity.enums.ActivityBaseTypeDictEnum;
import com.qqtech.qquan.activity.model.ActivityBase;
import com.qqtech.qquan.activity.model.ActivityBaseQuery;
import com.qqtech.qquan.activity.model.ActivityBaseVo;
import com.qqtech.qquan.activity.service.ActivityBaseService;
import com.qqtech.qquan.activity.service.ActivitySignService;
import com.qqtech.qquan.circle.model.CircleBaseVo;
import com.qqtech.qquan.circle.service.CircleBaseService;
import com.qqtech.qquan.sys.controller.BaseAgentController;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.service.UserBaseService;


@Controller
@RequestMapping(value = "/activity/base")
public class ActivityBaseController extends BaseAgentController {

	@Resource
	private ActivityBaseService activityBaseService;
	@Resource
	private ActivitySignService activitySignService;
	@Resource
	private UserBaseService userBaseService;
	
	@Resource
	private CircleBaseService circleBaseService;
	
	@Override
	protected BaseService<ActivityBase> getBaseService() {
		return activityBaseService;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "/activity/activityBase-list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(ActivityBaseQuery query, HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(),query.getSize(), new PageOrder(Direction.DESC, "id"));
		Page<ActivityBaseVo> page = activityBaseService.queryPageList(query, pageKit);
		UserBaseVo userBaseVo=null;
		CircleBaseVo  circleBaseVo=null;
		for (ActivityBaseVo activityBaseVo : page) {
			if(ActivityBaseTypeDictEnum.CIRCLE.getCode()==activityBaseVo.getType()){
				circleBaseVo=circleBaseService.getById(activityBaseVo.getBizzId());
				if(null!=circleBaseVo){
					activityBaseVo.setSponsorName(circleBaseVo.getName());
				}
			}else if(ActivityBaseTypeDictEnum.COMMUNITY.getCode()==activityBaseVo.getType()){
				userBaseVo=userBaseService.getById(activityBaseVo.getCreatorId());
				activityBaseVo.setSponsorName(userBaseVo.getNick());
			}
			activityBaseVo.setSignCount(activitySignService.getSignCount(activityBaseVo.getId()));//报名人数
		}
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}
	
	/**
	 * 显示
	 * 
	 * @param isShow
	 * @return
	 */
	@RequestMapping(value = "/receive")
	@ResponseBody
	public Result receive(Integer id) {
		Result ret = new Result();
		ActivityBaseVo activityBaseVo =new ActivityBaseVo();
		activityBaseVo.setId(id);
		activityBaseVo.setIsShow(YesNoEnum.YES.getCode());
		activityBaseService.updateById(activityBaseVo);
		return ret;
	}
	
	/**
	 * 不显示
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/unclaimed")
	@ResponseBody
	public Result unclaimed(Integer id) {
		Result ret = new Result();
		ActivityBaseVo activityBaseVo =new ActivityBaseVo();
		activityBaseVo.setId(id);
		activityBaseVo.setIsShow(YesNoEnum.NO.getCode());
		activityBaseService.updateById(activityBaseVo);
		return ret;
	}
	
	/**
	 * 删除
	 * 
	 * @param del
	 * @return
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Result delete(Integer id) {
		Result ret = new Result();
		activityBaseService.deleteById(id);
		return ret;
	}
	/**
	 * 推荐活动
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/recommend")
	@ResponseBody
	public Result recommend(Integer id, Integer recommendOidx) {
		Result ret = new Result();
		int updateCount = activityBaseService.setRecommend(id, recommendOidx);
		if(updateCount!=1){
			ret.addErr("推荐活动失败");
		}
		return ret;
	}
	/**
	 * 取消推荐活动
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/unRecommend")
	@ResponseBody
	public Result unRecommend(Integer id) {
		Result ret = new Result();
		int updateCount = activityBaseService.setUnRecommend(id);
		if(updateCount!=1){
			ret.addErr("取消推荐活动失败");
		}
		return ret;
	}
}
